package edu.pku.sei.transformation.editor.views.figures;

import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.GroupBoxBorder;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

public class SimpleRuleFigure extends RuleFigure {
	private Figure varArea = new Figure();

	public SimpleRuleFigure() {
		XYLayout layout = new XYLayout();
		setLayoutManager(layout);
		setBackgroundColor(this.backgroundColor);
		this.add(nameArea);
		nameArea.setLocation(new Point(0, 0));
		this.add(varArea);
		varArea.setLayoutManager(new ToolbarLayout(false));

	}

	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		nameArea.setLocation(new Point(rect.x + rect.width / 8, rect.y));
		nameArea
				.setSize(rect.width * 3 / 4, nameArea.getPreferredSize().height);
		varArea.setLocation(new Point(rect.x + rect.width / 8, rect.y
				+ nameArea.getSize().height));
		varArea.setSize(rect.width * 3 / 4, varArea.getPreferredSize().height);
	}

	@Override
	public Dimension getPreferredSize(int wHint, int hHint) {
		return nameArea.getPreferredSize();
	}

	public void addVariable(Label l) {
		this.varArea.add(l);
	}

	public void addVariableGroup(String name, List<Label> labels) {
		Figure group = new Figure() {
			@Override
			public Dimension getPreferredSize(int wHint, int hHint) {
				return new Dimension(wHint, super
						.getPreferredSize(wHint, hHint).height);
			}
		};
		group.setLayoutManager(new ToolbarLayout(false));
		group.setBorder(new GroupBoxBorder(name));
		varArea.add(group);
		for (Label label : labels) {
			group.add(label);
		}
		group.setSize(this.bounds.width * 3 / 4,
				group.getPreferredSize().height);
	}

	public void cleanVarArea() {
		this.varArea.removeAll();
	}
}
